<?
//phpinfo();
//die();

$cms_dir = str_replace('/exec', '', realpath(dirname(__FILE__)));
include ($cms_dir.'/config.php');
include ($cms_dir.'/phplib/db_mysql.php');

include ($cms_dir.'/includes/functions_parser.php');

$db = new DB_Sql();
$db->connect();

/*
// перевіряємо чи id в книг правильні
	$res = $db->qresult("SELECT a.title, b.id as book_id,b.author_id, b.title as book_title, b.user_add
						FROM `articles` a, books b
						WHERE a.book_id = b.id
						AND a.book_id >0
						AND a.title <> b.title
					");
	
// перевіряємо чи книга не складається з двох частин	
	$res = $db->qresult("SELECT a.title, b.id as book_id,b.author_id, b.title as book_title, b.user_add
						FROM `articles` a, books b
						WHERE a.book_id = b.id
						AND a.book_id >0
						AND a.title like '%.%'
					");
	
	// перевіряємо чи у книги є book_id 
	$res = $db->qresult("SELECT a.title,  a.header, a.id, a.author_id
						FROM `articles` a
						WHERE a.book_id =0
						
					");
	
	// перевіряємо чи у книги є author_id 
	$res = $db->qresult("SELECT a.title, a.header, a.id, a.book_id
						FROM `articles` a
						WHERE a.author_id =0
						
					");
		
	*/
/*
$res = $db->qresult("SELECT count(*) as cnt, s.book_id FROM shops s, articles a WHERE a.book_id=s.book_id AND  s.book_id=35904  GROUP BY s.book_id ORDER BY cnt DESC LIMIT 100");

	echo "<pre>";
	print_r($res);
	*/
$start = microtime(1);


###############
### query 1 ###
###############

/*
$users_id = $db->qcol("SELECT id FROM users u where  u.sex =2
AND u.birthday_year <=1992
AND u.birthday_year >=1986 ");

$articles_id = $db->qcol("SELECT article_id FROM comments c where user_id IN (".join(',',$users_id).") ");



$db->query("SELECT * FROM articles a WHERE a.active =1
AND a.lang_id =2
AND a.section_id =69
AND a.id NOT 
IN ( 348, 1907, 698, 299, 335, 181, 2053, 49, 842, 707, 274, 200, 1292, 37, 41, 594, 474, 578, 60, 836, 826, 679, 1053, 540, 645, 395 ) 
AND a.id IN (".join(',',$articles_id).")
GROUP BY a.id
ORDER BY a.best_count DESC 
");



$db->query("SELECT a . * 
FROM articles a, users u, comments c
WHERE c.user_id = u.id
AND a.active =1
AND a.lang_id =2
AND a.section_id =69
AND a.id NOT 
IN ( 348, 1907, 698, 299, 335, 181, 2053, 49, 842, 707, 274, 200, 1292, 37, 41, 594, 474, 578, 60, 836, 826, 679, 1053, 540, 645, 395 ) 
AND 1 
AND u.sex =2
AND u.birthday_year <=1992
AND u.birthday_year >=1986
GROUP BY a.id
ORDER BY best_count DESC"); 

*/


###############
### query 2 ###
###############


$articles_id = $db->qcol("SELECT c.article_id FROM users u,comments c WHERE c.user_id=u.id  AND u.sex=1  AND  u.birthday_year <= 1992  AND  u.birthday_year >= 1986");



$db->query("SELECT * FROM articles a WHERE a.active =1
AND a.lang_id =2
AND a.section_id =69
 AND  (a.genre = 'detectives' OR a.genre = 'historical_books' OR a.genre = 'classic' OR a.genre = 'mystery_horror' OR a.genre = 'adventures' OR a.genre = 'sentimental_novel' OR a.genre = 'comic_literature' OR a.genre = 'other' OR a.genre = 'without_genre')
GROUP BY a.id
ORDER BY a.best_count DESC LIMIT 0,10");


/*

echo "SELECT * FROM articles a WHERE a.active =1
AND a.lang_id =2
AND a.section_id =69
AND a.body <> ''
AND a.id IN (SELECT c.article_id FROM users u,comments c WHERE c.user_id=u.id  AND u.sex=1  AND  u.birthday_year <= 1992  AND  u.birthday_year >= 1986)
GROUP BY a.id
ORDER BY a.best_count DESC LIMIT 0,10";
*/

/*
$db->query("SELECT *
FROM articles a
WHERE a.active =1
AND a.lang_id =2
AND a.section_id =69
AND a.body <> ''
AND a.id
IN (

SELECT c.article_id
FROM users u, comments c
WHERE c.user_id = u.id
AND u.sex =1
AND u.birthday_year <=1992
AND u.birthday_year >=1986
)
GROUP BY a.id
ORDER BY a.best_count DESC
LIMIT 0 , 10"); 
*/


###############
### query 2 ###
###############


while ($db->next_record()) 
{
//	echo $db->f('title')."111<br>";
	
	//$db->exec("UPDATE comments SET lang_id=2 WHERE article_id=".$db->f('id'));
	//echo "UPDATE comments SET lang_id=1 WHERE article_id=".$db->f('id')." <br>";
}


echo microtime(1)- $start;

?>